const httpConfig = require('../configs/httpConfig');

module.exports = class {
    constructor() {
    }

    getRankList(cb) {
        this.requestGet(`${httpConfig.URL}${httpConfig.ROUTE.GET.GET_RANK_LIST}`, (ret) => {
            let data = JSON.parse(ret);
            cb(data);
        })
    }

    commitRank(name, score, cb) {
        let param = `?name=${name}&score=${score}`;
        this.requestGet(`${httpConfig.URL}${httpConfig.ROUTE.GET.COMMIT_RANK}${param}`, () => {
            cb();
        });
    }

    requestGet(url, callback) {
        // console.log(self.ip + url)
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                if (xhr.status >= 200 && xhr.status < 400) {
                    let response = xhr.responseText;
                    callback(response);
                } else {
                    console.error("请求失败", url);
                }
            }
        };
        xhr.open("GET", url, true);
        xhr.send();
    }
}